﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmAddEditShift : Form
    {
        Form caller;
        Shift shift;
        List<Department> departments;
        List<Title> titles;
        List<Skill> skills;

        public frmAddEditShift(Form Caller)
        {
            caller = Caller;

            InitializeComponent();

            cmbStartDay.Items.Clear();
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Sunday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Monday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Tuesday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Wednesday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Thursday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Friday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Saturday.ToString());
        }

        public frmAddEditShift(Form Caller, Shift editShift)
        {
            caller = Caller;
            shift = editShift;

            InitializeComponent();

            cmbStartDay.Items.Clear();
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Sunday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Monday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Tuesday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Wednesday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Thursday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Friday.ToString());
            cmbStartDay.Items.Add(GeneralClasses.DayOfWeek.DOW.Saturday.ToString());
        }

        public void refresh()
        {
            departments = Department.loadAllDepartments();
            cmbDepartment.Items.Clear();
            foreach (Department item in departments)
            {
                cmbDepartment.Items.Add(item.Name);
            }

            titles = Title.getAllTitles();
            cmbTitle.Items.Clear();
            foreach (Title item in titles)
            {
                cmbTitle.Items.Add(item.Name);
            }

            skills = Skill.getAllSkills();
            clbSkills.Items.Clear();
            foreach (Skill item in skills)
            {
                clbSkills.Items.Add(item.Name);
            }

            if (shift != null)
            {
                txtShiftName.Text = shift.Name;
                dtpStartTime.Value = shift.StartTime;
                dtpEndTime.Value = shift.EndTime;
                cmbStartDay.Text = shift.StartTime.DayOfWeek.ToString();

                foreach (Skill item in shift.Skills)
                {
                    clbSkills.SetItemChecked(clbSkills.Items.IndexOf(item.Name), true);
                }
                if (shift.getDepartmentID > 0)
                {
                    foreach (Department department in departments)
                    {
                        if (shift.getDepartmentID == department.DepartmentID)
                        {
                            cmbDepartment.Text = department.Name;
                            break;
                        }
                    }
                }
            }
        }

        private void btnDepartments_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmDepartments departments = new frmDepartments(this);
            departments.Show();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmAddEditShift_FormClosing(object sender, FormClosingEventArgs e)
        {
            caller.Show();
        }

        private void btnAddShift_Click(object sender, EventArgs e)
        {
            if (txtShiftName.Text == "")
            {
                MessageBox.Show("Please enter a name for the shift.");
                txtShiftName.Focus();
            }
            else if (cmbDepartment.Text == "")
            {
                MessageBox.Show("Please select a department.");
                cmbDepartment.Focus();
            }
            else if (cmbStartDay.Text == "")
            {
                MessageBox.Show("Please select a start day.");
                cmbStartDay.Focus();
            }
            else if (cmbTitle.Text == "")
            {
                MessageBox.Show("Please select a title.");
                cmbDepartment.Focus();
            }
            else
            {
                List<Department> departments = Department.loadAllDepartments();
                Department department = new Department();

                foreach (Department item in departments)
                {
                    if (item.Name == (string) cmbDepartment.SelectedItem)
                    {
                        department = item;
                        break;
                    }
                }

                if (shift == null)
                {
                    shift = new Shift();
                }

                shift.Name = txtShiftName.Text;
                shift.DayOfWeek = new GeneralClasses.DayOfWeek(cmbStartDay.Text);
                shift.StartTime = dtpStartTime.Value;
                shift.EndTime = dtpEndTime.Value;
                shift.getDepartmentID = department.DepartmentID;
                /*shift.Departments = new List<Department>();
                shift.Departments.Add(department);*/

                shift.Skills.Clear();
                foreach (object item in clbSkills.CheckedItems)
                {
                    string name = item.ToString();
                    foreach (Skill skill in skills)
                    {
                        if (skill.Name == name)
                        {
                            shift.Skills.Add(skill);
                            break;
                        }
                    }
                }

                this.Close();

                //The Shifts shouldn't be saved in the database since they
                //are already in the masterlist
                /*
                if (shift.saveData() >= 0)
                {
                    MessageBox.Show("Added shift to database.");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Problem adding shift to database.");
                    shift = null;
                }
                */
            }
        }

        private void frmAddEditShift_Shown(object sender, EventArgs e)
        {
            refresh();
        }

        private void btnTitles_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmTitles titles = new frmTitles(this);
            titles.Show();
        }

        private void btnSkills_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmSkills skills = new frmSkills(this);
            skills.Show();
        }

        private void txtShiftName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtShiftName, true, true, true, true, null);
        }
    }
}
